package pl.olap.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.HashMap;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableModel;

import pl.olap.dao.Movie;
import pl.olap.dao.Person;
import pl.olap.dao.Production;
import pl.olap.persistence.SearchUtil;

public class RightPanel extends JPanel {

	/** serialization uid */
	private static final long serialVersionUID = 7299355382502679317L;

	private JTable table = null;

	private Box hId;
	private JLabel hLId = new JLabel("Id: ");
	private JLabel hLIdWartosc = new JLabel("");

	private Box hTytul;
	private JLabel hLTytul = new JLabel("Tytul: ");
	private JLabel hLTytulWartosc = new JLabel("");

	private Box hOpis;
	private JLabel hLOpis = new JLabel("Opis: ");
	private JTextArea hLOpisWartosc = new JTextArea("");

	private Box hDataPremiery;
	private JLabel hLDataPremiery = new JLabel("Data premiery: ");
	private JLabel hLDataPremieryWartosc = new JLabel("");

	private Box hProdukcje;
	private JLabel hLProdukcje = new JLabel("Produkcja: ");
	private JLabel hLProdukcjeWartosc = new JLabel("");

	private Box hOcena;
	private JLabel hLOcena = new JLabel("Ocena: ");
	private JLabel hLOcenaWartosc = new JLabel("");

	private Box vCalosc;

	private JTable tableAktorzy;
	private JScrollPane pane = null;
	private JScrollPane allScrollPane = null;

	private String[] columnNames = { "Lp.", "Imie", "Nazwisko" };

	public RightPanel() {
		// ustawiam border
		Border b = BorderFactory.createLoweredBevelBorder();
		setBorder(b);

		hId = Box.createHorizontalBox();
		hId.add(hLId);
		hId.add(hLIdWartosc);
		hTytul = Box.createHorizontalBox();
		hTytul.add(hLTytul);
		hTytul.add(hLTytulWartosc);
		hOpis = Box.createHorizontalBox();
		JScrollPane scPane = new JScrollPane(hLOpisWartosc);
		hLOpisWartosc.setEnabled(false);
		hLOpisWartosc.setLineWrap(true);
		hLOpisWartosc.setWrapStyleWord(true);
		hLOpisWartosc.setSize(300, 100);
		hLOpisWartosc.setMaximumSize(new Dimension(300, 100));
		hLOpisWartosc.setPreferredSize(new Dimension(300, 100));
		//hLOpisWartosc.setRows(10);
		hOpis.add(hLOpis);
		hOpis.add(scPane);
		hDataPremiery = Box.createHorizontalBox();
		hDataPremiery.add(hLDataPremiery);
		hDataPremiery.add(hLDataPremieryWartosc);
		hProdukcje = Box.createHorizontalBox();
		hProdukcje.add(hLProdukcje);
		hProdukcje.add(hLProdukcjeWartosc);
		hOcena = Box.createHorizontalBox();
		hOcena.add(hLOcena);
		hOcena.add(hLOcenaWartosc);

		vCalosc = Box.createVerticalBox();
		vCalosc.add(hId);
		vCalosc.add(hTytul);
		vCalosc.add(hOpis);
		vCalosc.add(hDataPremiery);
		vCalosc.add(hProdukcje);
		vCalosc.add(hOcena);

		// dodajemy tabele z aktorami
		Object[][] data = new Object[1][3];
		this.table = new JTable(data, columnNames);
		pane = new JScrollPane(table);
		table.setFillsViewportHeight(true);
		vCalosc.add(pane);
		allScrollPane = new JScrollPane(vCalosc);
		add(allScrollPane, BorderLayout.CENTER);

	}

	public void setMovie(Movie m) {
		if (m == null) {
			return;
		}
		hLIdWartosc.setText(m.getIdMovie().toString());
		hLTytulWartosc.setText(m.getTitle());
		hLOpisWartosc.setText(m.getDescription());
		SimpleDateFormat d = new SimpleDateFormat("dd-MM-yyyy");
		if (m.getPremiere() != null) {
			if (m.getPremiere().getDateOfPremiere() != null) {
				hLDataPremieryWartosc.setText(d.format(m.getPremiere()
						.getDateOfPremiere()));
			}
		} else {
			hLDataPremieryWartosc.setText("");
		}
		if (m.getProductions() != null) {
			String t = "";
			for (Production p : m.getProductions()) {
				t += p.getName() + ", ";
			}
			hLProdukcjeWartosc.setText(t);
		} else {
			hLProdukcjeWartosc.setText("");
		}

		hLOcenaWartosc.setText(m.getRating().toString());

		pane.remove(table);
		vCalosc.remove(pane);

		Object[][] data = new Object[m.getPersons().size()][3];
		int i = 0;
		for (Person p : m.getPersons()) {
			data[i][0] = i+1;
			data[i][1] = p.getFirstName();
			data[i][2] = p.getLastName();
			i++;
		}
		this.table = new JTable();
		DefaultTableModel tableModel = new DefaultTableModel(data, columnNames) {

			@Override
			public boolean isCellEditable(int row, int column) {
				// all cells false
				return false;
			}
		};
		table.setModel(tableModel);

		// table.setEnabled(false);

		pane = new JScrollPane(table);
		vCalosc.add(pane);

		this.updateUI();
		this.repaint();
	}
}
